package su.samcom.calc.model.dao;

import org.springframework.stereotype.Repository;
import su.samcom.calc.model.entity.*;

/**
 * Created with IntelliJ IDEA.
 * User: Владислав
 * Date: 17.11.13
 * Time: 4:51
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class PropertyDaoImpl extends BaseDaoImpl<Property> implements PropertyDao {

    /*@Override
    public List<Property> getAll() {
        String sql = "select p from Property p";
        return sessionFactory.getCurrentSession().createQuery(sql).list();
    }*/

    @Override
    public Property getProperty(String key) {
        return (Property) sessionFactory.getCurrentSession().get(Property.class, key);
    }

    @Override
    public void initDatabase() {

        sessionFactory.getCurrentSession().createQuery("delete from Property").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from Route").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from PriceStrategy").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from Price").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from PriceGroup").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from Location").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from Country").executeUpdate();

        Country kz = new Country();
        kz.setName("Казахстан");
        sessionFactory.getCurrentSession().persist(kz);

        Country ru = new Country();
        ru.setName("Россия");
        sessionFactory.getCurrentSession().persist(ru);

        Location astana = new Location();
        astana.setCountry(kz);
        astana.setName("Астана");
        sessionFactory.getCurrentSession().persist(astana);

        Location almati = new Location();
        almati.setCountry(kz);
        almati.setName("Алматы");
        sessionFactory.getCurrentSession().persist(almati);

        Location aktobe = new Location();
        aktobe.setCountry(kz);
        aktobe.setName("Актобе");
        sessionFactory.getCurrentSession().persist(aktobe);

        Location uralsk = new Location();
        uralsk.setCountry(kz);
        uralsk.setName("Уральск");
        sessionFactory.getCurrentSession().persist(uralsk);

        Location moscow = new Location();
        moscow.setCountry(ru);
        moscow.setName("Москва");
        sessionFactory.getCurrentSession().persist(moscow);

        Location piter = new Location();
        piter.setCountry(ru);
        piter.setName("Санкт-Петербург");
        sessionFactory.getCurrentSession().persist(piter);

        Location kazan = new Location();
        kazan.setCountry(ru);
        kazan.setName("Казань");
        sessionFactory.getCurrentSession().persist(kazan);

        Location saratov = new Location();
        saratov.setCountry(ru);
        saratov.setName("Саратов");
        sessionFactory.getCurrentSession().persist(saratov);

        Location sochi = new Location();
        sochi.setCountry(ru);
        sochi.setName("Сочи");
        sessionFactory.getCurrentSession().persist(sochi);

        Location ulianovsk = new Location();
        ulianovsk.setCountry(ru);
        ulianovsk.setName("Ульяновск");
        sessionFactory.getCurrentSession().persist(ulianovsk);

        Location samara = new Location();
        samara.setCountry(ru);
        samara.setName("Самара");
        sessionFactory.getCurrentSession().persist(samara);

        PriceGroup separatePriceGroup = new PriceGroup();
        sessionFactory.getCurrentSession().persist(separatePriceGroup);
        PriceGroup generalPriceGroup = new PriceGroup();
        sessionFactory.getCurrentSession().persist(generalPriceGroup);

        Price separate1 = new Price();
        separate1.setCbm(1.0);
        separate1.setLength(0.2);
        separate1.setWeight(0.1);
        separate1.setPrice(8000);
        separate1.setPriceGroup(separatePriceGroup);
        sessionFactory.getCurrentSession().persist(separate1);

        Price separate2 = new Price();
        separate2.setCbm(30.0);
        separate2.setLength(5.0);
        separate2.setWeight(5.0);
        separate2.setPrice(65000);
        separate2.setPriceGroup(separatePriceGroup);
        sessionFactory.getCurrentSession().persist(separate2);

        Price separate3 = new Price();
        separate3.setCbm(45.0);
        separate3.setLength(7.0);
        separate3.setWeight(10.0);
        separate3.setPrice(80000);
        separate3.setPriceGroup(separatePriceGroup);
        sessionFactory.getCurrentSession().persist(separate3);

        Price general1 = new Price();
        general1.setCbm(10.0);
        general1.setLength(3.0);
        general1.setWeight(1.5);
        general1.setPrice(25000);
        general1.setPriceGroup(generalPriceGroup);
        sessionFactory.getCurrentSession().persist(general1);

        Price general2 = new Price();
        general2.setCbm(45.0);
        general2.setLength(7.0);
        general2.setWeight(10.0);
        general2.setPrice(65000);
        general2.setPriceGroup(generalPriceGroup);
        sessionFactory.getCurrentSession().persist(general2);

        PriceStrategy priceStrategy = new PriceStrategy();
        priceStrategy.setGeneralCargo(generalPriceGroup);
        priceStrategy.setSeparateCargo(separatePriceGroup);
        sessionFactory.getCurrentSession().persist(priceStrategy);

        Route route = new Route();
        route.setLocationA(samara);
        route.setLocationB(astana);
        route.setPriceStrategy(priceStrategy);
        sessionFactory.getCurrentSession().persist(route);

    }

    @Override
    protected Class<Property> getGeneric() {
        return Property.class;
    }
}
